using System;
using System.Linq;
using System.Windows.Forms;
using Comteck.Dto.Orders;
using Comteck.Ktt.Services.Orders;
using Comteck.Linq;
using Comteck.Localization;
using Comteck.Winforms;
using Comteck.Winforms.Data;
using Comteck.Winforms.Forms;
using Comteck.Winforms.Infrastructure;
using DevExpress.XtraBars;
using ExpressMapper.Extensions;
using Microsoft.Extensions.DependencyInjection;

namespace Comteck.Ktt.WinUI.Orders {
  public partial class FrmSelfOrgAdmin : FrmBase {
    private readonly ISelfOrgService selfOrgService;
    private CKObservableCollection<SelfOrgDto> mainDataList;

    public FrmSelfOrgAdmin(ISelfOrgService selfOrgService) {
      this.selfOrgService = selfOrgService;
      InitializeComponent();
    }

    private async void FrmSelfOrgAdmin_Load(object sender, EventArgs e) {
      if (this.DesignMode) {
        return;
      }

      gcMain.DataSource = mainDataList = new CKObservableCollection<SelfOrgDto>(this);
      colCreateTime.FormatDate();

      // 自提点标签
      var selfOrgTagDtoList = await ServerDbDataHelper.GetAllSelfOrgTagAsync();
      await BindHelper.BindSelfOrgTag(searchTagId, null, selfOrgTagDtoList);
      await BindHelper.BindSelfOrgTag(rilpTagIds, null, selfOrgTagDtoList);
    }

    private async void btnSearch_ItemClick(object sender, ItemClickEventArgs e) {
      // search model
      var searchModel = new SelfOrgQuery() {
        Name = searchName.EditValue.To<string>(),
        Contacts = searchContacts.EditValue.To<string>(),
        Phone = searchPhone.EditValue.To<string>(),
        TagId = searchTagId.EditValue.To<int?>(),
      };

      // search
      ajaxLoad.Show();
      var result = await selfOrgService.GetListAsync(searchModel);
      ajaxLoad.Hide();

      result.ToList().MapTo(mainDataList, x => x.Id, x => x.Id);
    }

    private void searchName_KeyDown(object sender, KeyEventArgs e) {
      if (e.KeyCode == Keys.Enter) {
        btnSearch.PerformClick();
      }
    }

    private void btnAdd_ItemClick(object sender, ItemClickEventArgs e) {
      var form = SysGlobal.ServiceProvider.GetRequiredService<FrmSelfOrgAction>();
      form.AddOrUpdateAction = (entity) => btnSearch.PerformClick();
      form.Owner = this;
      form.ShowDialog();

    }

    private void btnEdit_ItemClick(object sender, ItemClickEventArgs e) {
      if (!(gvMain.GetFocusedRow() is SelfOrgDto model)) {
        return;
      }

      var form = SysGlobal.ServiceProvider.GetRequiredService<FrmSelfOrgAction>();
      form.EntityId = model.Id;
      form.AddOrUpdateAction = (entity) => btnSearch.PerformClick();
      form.Owner = this;
      form.ShowDialog();

    }

    private async void btnDelete_ItemClick(object sender, ItemClickEventArgs e) {
      if (!(gvMain.GetFocusedRow() is SelfOrgDto model)) {
        return;
      }

      var dialog = this.ShowMsg(BaseResources.Delete_Selected_Confirm, "提示", MessageBoxButtons.YesNo,
        MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
      if (dialog == DialogResult.No) {
        return;
      }

      try {
        ajaxLoad.Show();
        await selfOrgService.DeleteAsync(model.Id);
        this.ShowMsg(BaseResources.Delete_Success);
        ajaxLoad.Hide();
        btnSearch.PerformClick();
      } catch (Exception ex) {
        this.ShowMsg(BaseResources.Delete_Failed_Reason, ex.Message);
      } finally {
        ajaxLoad.Hide();
      }
    }

    private void btnExport_ItemClick(object sender, ItemClickEventArgs e) {
      gvMain.ExportDialog();
    }

    private void btnClose_ItemClick(object sender, ItemClickEventArgs e) {
      this.Close();
    }
  }
}
